"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : 线程池打开下，测试兼容mysql侧join语法
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、建表
    4、插入数据
    5、使用join两个表查询，不加on
    6、切换到A库下使用join查询，不加on条件
    7、清理环境
Expect      :
    1、成功
    2、成功
    3、自动加载成功,无需手动创建
    4、成功
    5、成功
    6、合理报错
    7、清理环境完成
History     :modified by z@wan005,2022.08.29，om数据库目前实现自动加载dolphin插件，
             因此删除用例中拷贝插件依赖文件步骤
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PluginTest07(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0007'
        self.tb_name = 'tb_plugin0007'
        self.default_value = self.common.show_param('enable_thread_pool')
        sql_res = self.sh_primy.execut_db_sql('alter system set '
                                              'enable_thread_pool to off;')
        self.log.info(sql_res)
        self.sh_primy.restart_db_cluster()

    def test_dolphin(self):
        text = '----step1:创建b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '---step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step3:建表;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name},{self.tb_name}_01;\
            create table {self.tb_name}(i integer,j integer,t text(10));\
            create table {self.tb_name}_01(i integer,k integer);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEquals(2, sql_res.count(self.constant.TABLE_CREATE_SUCCESS),
                          '执行失败' + text)

        text = '-----step4:插入数据;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''insert into {self.tb_name} values (1, 4, 'one');
            insert into {self.tb_name} values (2, 3, 'two');
            insert into {self.tb_name} values (0, null, 'zero');
            insert into {self.tb_name}_01 values (1, -1);
            insert into {self.tb_name}_01 values (null, 0);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEquals(5, sql_res.count(self.constant.INSERT_SUCCESS_MSG),
                          '执行失败:' + text)

        text = '--step5:使用join两个表查询，不加on;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''select '' as "xxx", * from {self.tb_name} join \
            {self.tb_name}_01 order by {self.tb_name}.i;\
            select '' as "xxx", * from {self.tb_name} inner join \
            {self.tb_name}_01;'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('6 rows', sql_res, '执行失败' + text)

        text = '--step6:切换到A库下使用join查询，不加on条件;expect:报错---'
        self.log.info(text)
        sql_cmd = f'''select '' as "xxx", * from {self.tb_name} join \
            {self.tb_name}_01 order by {self.tb_name}.i;\
            select '' as "xxx", * from {self.tb_name} inner join \
            {self.tb_name}_01;'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn('ERROR', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '--step7:清理环境;expect:清理环境完成--'
        self.log.info(text)
        alter_cmd = self.sh_primy.execut_db_sql(f'alter system set '
                                                f'enable_thread_pool to '
                                                f'{self.default_value};')
        self.log.info(alter_cmd)
        self.sh_primy.restart_db_cluster()
        status = self.sh_primy.get_db_cluster_status()
        self.log.info(status)
        sql_cmd = f"drop database if exists {self.db_name};"
        sql_res2 = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res2)
        self.log.info('断言teardown成功')
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, alter_cmd,
                      '执行失败' + text)
        self.assertTrue(self.constant.DROP_DATABASE_SUCCESS in sql_res2,
                        '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
